<?php

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\ComponentResource\Pages;
use App\Filament\Admin\Resources\ComponentResource\RelationManagers;
use App\Models\Admin;
use App\Models\Component;
use Filament\Forms;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TagsInput;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Support\Str;
use App\Models\ComponentCategory;
use Filament\Actions\Action;
use CodeWithDennis\FilamentSelectTree\SelectTree;
use App\Models\Category;
use Dotswan\FilamentCodeEditor\Fields\CodeEditor;
use Filament\Tables\Columns\ImageColumn;



class ComponentResource extends Resource
{
    protected static ?string $model = Component::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static ?string $modelLabel = '组件';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('chinese_title')->label('中文标题'),
                TextInput::make('title')->label('标题')->live(onBlur: true)->afterStateUpdated(fn (string $operation, $state, Forms\Set $set) => $operation === 'create' ? $set('slug', Str::slug($state)) : null),
                TextInput::make('slug')->label('别名')
                    ->dehydrated()
                    ->required()
                    ->maxLength(255)
                    ->unique(Component::class, 'slug', ignoreRecord: true),
                MarkdownEditor::make('description')
                    ->required()
                    ->label('描述'),
                FileUpload::make('screenshot')->image()->label('截图'),
                Toggle::make('is_visible')->default(true)->label('是否可见'),
                SelectTree::make('categories')->label('选择分类')
                    ->relationship('categories', 'title', 'parent_id'),
                Select::make('admin_id')->label('管理员')
                    ->required()
                    ->options(
                        Admin::all()->pluck('name', 'id')->toArray(),
                    ),
                TextInput::make('download_path')->label('下载路径')->required(),
                Select::make('count')->options([
                    1 => '1',
                    2 => '2',
                    3 => '3',
                    4 => '4',
                    5 => '5',
                ])->label('数量'),
                TagsInput::make('tags')->label('标签'),
                Select::make('viewports')->options([
                    'mobile' => 'mobile',
                    'desktop' => 'desktop',
                    'tablet' => 'tablet',
                ])->multiple(),
                Select::make('related')->label("相关组件")->options(Component::all()->pluck('title', 'id')->toArray())->multiple(),

            ])->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('id')->label('ID'),
                ImageColumn::make('screenshot')->label('截图'),
                TextColumn::make('title')->label('标题'),

            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListComponents::route('/'),
            'create' => Pages\CreateComponent::route('/create'),
            'edit' => Pages\EditComponent::route('/{record}/edit'),
        ];
    }
}
